<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>充值列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/public/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/public/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">

                <table class="layui-hide" id="test-table-reload" lay-filter="test-table-reload"></table>

                <script type="text/html" id="test-table-reload-toolbar">
                    <button class="layui-btn layui-btn-sm #{if !toolsUtils.checkBtn('/recharge/addRecharge')} layui-hide #{/if}" lay-event="add"><i class="layui-icon layui-icon-add-circle-fine"></i>添加充值类型</button>
                </script>

                <script type="text/html" id="test-table-reload-barDemo">
                    <a class="layui-btn layui-btn-xs #{if !toolsUtils.checkBtn('/recharge/rechargeCouponList')} layui-hide #{/if}" lay-event="edit"><i class="layui-icon layui-icon-set-fill"></i>福利配置</a>
                </script>
            </div>
        </div>
    </div>
</div>

<script src="/public/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/public/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'utils'], function () {
        var $ = layui.$,
                table = layui.table,
                admin = layui.admin;

        //方法级渲染
        table.render({
            elem: '#test-table-reload'
            , url: "/recharge/getRechargeList"
            , toolbar: '#test-table-reload-toolbar'
            , defaultToolbar: ['filter']
            , cellMinWidth: 80
            , cols: [[
                {field: 'id', title: '序号', fixed: true}
                , {field: 'orderIndex', title: '权重', edit: 'text'}
                , {field: 'amount', title: '充值金额'}
                , {field: 'usedCount', title: '充值人数'}
                , {field: 'couponName', title: '充值福利', width: 350}
                , {field: 'name', title: '创建人'}
                , {field: 'createTime', title: '创建时间', sort: true, templet: function (d) {
                        return "<div>" + layui.toDateString(d.createTime) + "</div>";
                    }}
                , {field: 'invalid', title: '展示状态', templet: invalid, width: 120}
                , {fixed: 'right', title: '操作', toolbar: '#test-table-reload-barDemo', width: 150}
            ]]
            , page: true
            , height: 520
        });

        //头工具栏事件
        table.on('toolbar(test-table-reload)', function (obj) {
            if (obj.event === 'add') {
                window.location.href = "/recharge/addRecharge";
            }
        });

        //监听单元格编辑
        table.on('edit(test-table-reload)', function(obj){
            var value = obj.value //得到修改后的值
                    ,data = obj.data; //得到所在行所有键值
            var params = {"orderIndex": value, "id": data.id};
            layer.confirm('是否修改权重', {icon: 7, title: '提示', skin: 'layui-layer-molv', anim: 4}, function () {
                $.post("/recharge/saveRecharge", params, function (result) {
                    if (result.code > 0) {
                        layer.msg(result.msg, {icon: 2});
                    } else {
                        layer.msg("修改权重成功", {icon: 1, time: 1000}, function () {
                            window.location.reload();
                        });
                    }
                });
            });
        });

        //监听行工具事件
        table.on('tool(test-table-reload)', function (obj) {
            var data = obj.data;
            if (obj.event === 'show') {
                layer.confirm('现在的充值金额是显示状态，是否更改为隐藏状态？', {icon: 7, title: '提示', skin: 'layui-layer-molv', anim: 4}, function () {
                    $.get("/recharge/updateInValid?id=" + data.id + "&invalid=1", function (result) {
                        if (result.code > 0) {
                            layer.msg(result.msg, {icon: 2});
                        } else {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                window.location.reload();
                            });
                        }
                    });
                });
            } else if (obj.event === 'hide') {
                layer.confirm('现在的充值金额是隐藏状态，是否更改为显示状态？', {icon: 7, title: '提示', skin: 'layui-layer-molv', anim: 4}, function () {
                    $.get("/recharge/updateInValid?id=" + data.id + "&invalid=0", function (result) {
                        if (result.code > 0) {
                            layer.msg(result.msg, {icon: 2});
                        } else {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                window.location.reload();
                            });
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                parent.layui.index.openTabsPage("/recharge/rechargeCouponList?rechargeId=" + data.id, "序号 "+ "<span style= 'color: red'>"+ data.id+ "</span>" +" 的福利配置");
            }
        });

        function invalid(data) {
            if(data.invalid === 0){
                return "<div class='layui-btn layui-btn-xs #{if !toolsUtils.checkBtn('/recharge/updateInValid')} layui-hide #{/if}' lay-event='show'>"+ "显示"+"</div>";
            }
            if(data.invalid === 1){
                return "<div class='layui-btn layui-btn-xs #{if !toolsUtils.checkBtn('/recharge/updateInValid')} layui-hide #{/if}' lay-event='hide'>"+ "隐藏"+"</div>";
            }
        }

    });
</script>
</body>
</html>